/**
 * 
 */
package com.hudie;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;


@SuppressWarnings("serial")

/**
 * @author Liyun
 *
 */
public class WeatherServlet extends HttpServlet{
	private List<List<String>> weatherList = null;
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {
		resp.setContentType("text/plain; charset=UTF-8");
		resp.setCharacterEncoding("UTF-8");
		URL url = new URL(
				"http://www.google.com/ig/api?weather=TianJin&hl=zh-cn");

		// 读取源码
		BufferedReader reader = new BufferedReader(new InputStreamReader(url
				.openStream(), "gb2312"));
		String xml = "";
		String line = "";
		while((line = reader.readLine())!= null)
			xml += line;
		try{
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			SAXReader saxReader = new SAXReader();
			
			InputSource is =new InputSource();
            is.setByteStream(new ByteArrayInputStream(xml.getBytes()));
            sp.parse(is, saxReader);
            
            weatherList=saxReader.getWeathList();
            
            resp.getWriter().println(weatherList.toArray()[2]);
            resp.getWriter().println(weatherList.toArray()[3]);
        }catch(Exception e){
        	e.printStackTrace();
        }finally{
        	reader.close();
        }
		
		
	}
	
	@Override
	public void init(){
		weatherList = new ArrayList<List<String>>();
	}

}
